﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;

using Engine.Result;

using Scheduler;

namespace Engine.Middlewares
{

    /// <summary>
    /// 测试逻辑管道
    /// </summary>
    [DebuggerStepThrough]
    public class TestResultPipe
    {
        private List<BlockingCollection<object>> _listValue;
        public readonly List<ChipResult> ChipResult = new List<ChipResult>();


        public TestResultPipe(int stCount)
        {
            _listValue = new List<BlockingCollection<object>>();
            for (int i = 0; i < stCount; i++)
            {
                _listValue.Add(new BlockingCollection<object>(1));
            }


        }



        [DebuggerStepThrough]
        public bool HasInBuffer(int st)
        {
            if (_listValue[st] != null)
            {
                return _listValue[st].Count > 0;
            }
            throw new NotImplementedException($"st:{st}");
        }

        [DebuggerStepThrough]
        public void AddToBuffer(int st, object dutPoint)
        {

            _listValue[st].Add(dutPoint);
        }
        [DebuggerStepThrough]
        public int Count(int st) => _listValue[st].Count;

        [DebuggerStepThrough]
        public bool NextWorker(int st)
        {
            return _listValue.ElementAtOrDefault(st + 1) != null;
        }

        
        public bool TryDequeue(int st, out object dutPoint)
        {
            while (_listValue[st].TryTake(out dutPoint, Timeout.Infinite))
            {
                return true;
            }

            return false;
        }


        [DebuggerStepThrough] public bool TryPeek(int st, out object dutPoint) => _listValue[st].TryTake(out dutPoint,Timeout.Infinite);



        //public void Add(DUTPoint item)
        //{
        //    _listValue[0].Enqueue(item);
        //}

        //public bool NextBegin(int st)
        //{
        //    if (_listValue[st] != null)
        //    {
        //        return _listValue[st].Count > 0;
        //    }
        //    throw new Exception();
        //}

        //public bool Take(int st, out  DUTPoint chip)
        //{
        //    chip = null;

        //    return true;
        //}

        //public bool NextPass(int st, int index)
        //{
        //    if (_listValue[st] == null) throw new Exception();

        //    _threadLocal.Value.Add(new DUTPoint(st,0));

        //    while (true)
        //    {
        //        while (_listValue[st].TryDequeue(out var chipPoint))
        //        {
        //            if (_listValue.ElementAtOrDefault(st + 1) != null)
        //            {
        //                _listValue[st + 1].Enqueue(chipPoint);
        //            }
        //            else
        //            {
        //                ChipResult.Add(new ChipResult() { Chip = chipPoint });
        //            }

        //        }

        //        Console.WriteLine($"st:{st}  饥饿=>" + Thread.CurrentThread.ManagedThreadId);
        //        Thread.Sleep(1000);
        //    }
        //}



    }
}